﻿using System;
using System.Collections;
using System.Text;

namespace MathExp
{
    public class Parser
    {
        private char[] words;
        private int index=0;
        public Object[] ParseExp(String expr)
        {
            ArrayList list = new ArrayList();
            words = expr.ToCharArray();
            // (a+b)*c+d
            char c = MoveNext();
            while (true)
            {
               
                if (c == '(') list.Add(Bracket.LEFT_BRACKET);
                else if (c == ')') list.Add(Bracket.RIGHT_BRACKET); 
                else if (Char.IsDigit(c))
                {
                    StringBuilder builder = new StringBuilder();
                    builder.Append(c);
                    //循环并取出数字
                    while (!IsEOF()) 
                    {
                        c = MoveNext();
                        if (Char.IsDigit(c) || c == '.')
                        {
                            builder.Append(c);
                            //MoveNext();
                            if (IsEOF())
                            {
                                break;
                            }
                            
                        }
                        else
                        {
                            c = Back();
                            break;
                        }
                    } 
                    list.Add(Decimal.Parse(builder.ToString()));
                }
                else if (c == '+')  list.Add(new Plus());//Is Word
                else if (c == '-') list.Add(new Minus());
                else if (c == '*') list.Add(new Multiple());
                else if (c == '/') list.Add(new Divide());
                else if (Char.IsLetter(c))
                {
                    StringBuilder builder = new StringBuilder();
                    builder.Append(c);
                    //循环并取出数字
                    while (!IsEOF())
                    {
                        c = MoveNext();
                        if (Char.IsLetter(c))
                        {
                            builder.Append(c);
                            //MoveNext();
                            if (IsEOF())
                            {
                                break;
                            }

                        }
                        else
                        {
                            c = Back();
                            break;
                        }
                    }
                    list.Add(builder.ToString());
                }
                if (IsEOF())
                {
                    break;
                }
                else
                {
                    c = MoveNext();
                }
            }
           
            return list.ToArray();
        }

        private char Back()
        {
            index--;
            return words[index];
        }
        
        private char GetChar()
        {
            return words[index];
        }
        private char Peek()
        {
            return words[index];
        }
        private char MoveNext()
        {
            return words[index++];
        }
        private bool IsEOF()
        {
            if (index < words.Length)
            {
                
                return false;
            }
            return true;
        }
        private bool IsEOF3()
        {
            if (index < words.Length)
            {

                return true;
            }
            return false;
        }
        private bool IsEOF2()
        {
            if (index+1 < words.Length)
            {
                index++;
                return true;
            }
            return false;
        }
        
    }
}
